package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.DriverManagement;
import com.woniuxy.entity.Vehicle;
import com.woniuxy.entity.dto.GoodsManagementDTO;
import com.woniuxy.entity.dto.PartnerDTO;
import com.woniuxy.mapper.DriverManagementMapper;
import com.woniuxy.mapper.PartnerMapper;
import com.woniuxy.service.DriverManagementService;
import com.woniuxy.util.PageData;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.ResponseEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DriverManagementServiceImpl implements DriverManagementService {

    @Resource
    private DriverManagementMapper driverManagementMapper;
    @Override
    public ResponseData<?> queryAllPartner(PageData<DriverManagement> pageData) {
        PageHelper.startPage(pageData.getPageNum(),pageData.getPageSize());
        List<DriverManagement> driverManagements = driverManagementMapper.queryAllDriver(pageData.getParam());

        for (DriverManagement driverManagement : driverManagements) {
            // 获取当前时间
            LocalDateTime currentTime = LocalDateTime.now();
            // 定义日期时间格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            // 格式化当前时间为字符串
            String formattedDateTime = currentTime.format(formatter);  //当前时间字符串

            String licenseExpiration = driverManagement.getLicenseExpiration(); //有效日期字符串

            // 解析字符串为LocalDate对象
            LocalDate nowTime = LocalDate.parse(formattedDateTime, formatter);

            LocalDate validTime = LocalDate.parse(licenseExpiration, formatter); //有效日期
            // 比对nowTime和validTime两个时间的大小
            if (!nowTime.isBefore(validTime)) {
                driverManagement.setDriverStatus("0");
                driverManagementMapper.updateDriverStatus(driverManagement);
            }
        }

        PageInfo pageInfo = new PageInfo(driverManagements);
        return new ResponseData<>().ok(pageInfo);
    }

    @Override
    public ResponseData<?> addDriver(DriverManagement driverManagement) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = dateFormat.parse(driverManagement.getLicenseDate());
        String formattedDate = dateFormat.format(date);
        driverManagement.setLicenseDate(formattedDate);
        Date date1 = dateFormat.parse(driverManagement.getLicenseExpiration());
        String formattedDate1 = dateFormat.format(date1);
        driverManagement.setLicenseExpiration(formattedDate1);

        driverManagementMapper.addDriver(driverManagement);
        return new ResponseData<>().ok();
    }

    @Override
    public ResponseData<?> updateDriver(DriverManagement driverManagement) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = dateFormat.parse(driverManagement.getLicenseDate());
        String formattedDate = dateFormat.format(date);
        driverManagement.setLicenseDate(formattedDate);
        Date date1 = dateFormat.parse(driverManagement.getLicenseExpiration());
        String formattedDate1 = dateFormat.format(date1);
        driverManagement.setLicenseExpiration(formattedDate1);
        driverManagementMapper.updateDriver(driverManagement);
        return new ResponseData<>().ok();
    }

    @Override
    public ResponseData<?> updateDriverStatus(DriverManagement driverManagement) {
        if (driverManagement.getDriverStatus().equals("0")){
            //启用 改成1
            LocalDateTime currentTime = LocalDateTime.now();
            // 定义日期时间格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            // 格式化当前时间为字符串
            String formattedDateTime = currentTime.format(formatter);  //当前时间字符串

            String licenseExpiration = driverManagement.getLicenseExpiration(); //有效日期字符串

            // 解析字符串为LocalDate对象
            LocalDate nowTime = LocalDate.parse(formattedDateTime, formatter);

            LocalDate validTime = LocalDate.parse(licenseExpiration, formatter); //有效日期
            // 比对nowTime和validTime两个时间的大小
            if (!nowTime.isBefore(validTime)) {
                return new ResponseData<>().fail(ResponseEnum.COMBINED_ORDER_ERROR3);//2011  驾照过期 无法启用
            }
            driverManagement.setDriverStatus("1");
            driverManagementMapper.updateDriverStatus(driverManagement);
            return new ResponseData<>().ok();
        }else if (driverManagement.getDriverStatus().equals("1")){
            //停用  修改成0
            List<Vehicle> vehicles = driverManagementMapper.queryDriverCar(driverManagement.getId().toString());
            if (vehicles != null && !vehicles.isEmpty()){
                return new ResponseData<>().fail(ResponseEnum.ORDER_ERROR);//2015  绑定车辆 无法停用
            }
            driverManagement.setDriverStatus("0");
            driverManagementMapper.updateDriverStatus(driverManagement);
            return new ResponseData<>().ok();
        }

        return new ResponseData<>().fail(ResponseEnum.FAIL);
    }


    @Override
    public ResponseData<?> batchUpdateStatus1(List<DriverManagement> driverManagements) {
        List<Integer> ids = new ArrayList<>();
        for (DriverManagement driverManagement : driverManagements) {
            //启用 改成1
            LocalDateTime currentTime = LocalDateTime.now();
            // 定义日期时间格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            // 格式化当前时间为字符串
            String formattedDateTime = currentTime.format(formatter);  //当前时间字符串

            String licenseExpiration = driverManagement.getLicenseExpiration(); //有效日期字符串

            // 解析字符串为LocalDate对象
            LocalDate nowTime = LocalDate.parse(formattedDateTime, formatter);

            LocalDate validTime = LocalDate.parse(licenseExpiration, formatter); //有效日期
            // 比对nowTime和validTime两个时间的大小
            if (!nowTime.isBefore(validTime)) {
                return new ResponseData<>().fail(ResponseEnum.COMBINED_ORDER_ERROR3);//2011  驾照过期 无法启用
            }
            ids.add(driverManagement.getId());
        }
        driverManagementMapper.batchUpdateDriverStatus(ids,"1");
        return new ResponseData<>().ok();
    }

    @Override
    public ResponseData<?> batchUpdateStatus0(List<DriverManagement> driverManagements) {
        List<Integer> ids = new ArrayList<>();
        for (DriverManagement driverManagement : driverManagements) {
            List<Vehicle> vehicles = driverManagementMapper.queryDriverCar(driverManagement.getId().toString());
            if (vehicles != null && !vehicles.isEmpty()){
                return new ResponseData<>().fail(ResponseEnum.ORDER_ERROR);//2015  绑定车辆 无法停用
            }
            ids.add(driverManagement.getId());
        }
        driverManagementMapper.batchUpdateDriverStatus(ids,"0");
        return new ResponseData<>().ok();
    }
}
